﻿package 
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import Utils.Directions;

	public class InputManager extends Contexted
	{
		// USEFULL KEYS
		var currentDirection:uint = Directions.NONE;

		public function InputManager(context:Context)
		{
			super(context);
		}

		// each new frame
		public function enterFrame(e:Event):void
		{

			switch (currentDirection)
			{
				case Directions.NONE :
					break;
				default :
					context.sender.onMoveAvatar(currentDirection);
			}
		}

		public function keyDownHandler(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.UP :
					currentDirection = Directions.UP;
					break;
				case Keyboard.DOWN :
					currentDirection = Directions.DOWN;
					break;
				case Keyboard.RIGHT :
					currentDirection = Directions.RIGHT;
					break;
				case Keyboard.LEFT :
					currentDirection = Directions.LEFT;
					break;
				case Keyboard.A :
				case Keyboard.Q :
					// set bomb
					context.sender.onPutBomb();
					break;
				case Keyboard.Z :
				case Keyboard.W :
					// explode bomb
					context.sender.onTriggerBomb();
					break;
			}
		}

		public function keyUpHandler(event:KeyboardEvent):void
		{
			switch (event.keyCode)
			{
				case Keyboard.UP :
					if (currentDirection == Directions.UP)
					{
						currentDirection = Directions.NONE;
					}
					break;
				case Keyboard.DOWN :
					if (currentDirection == Directions.DOWN)
					{
						currentDirection = Directions.NONE;
					}
					break;
				case Keyboard.RIGHT :
					if (currentDirection == Directions.RIGHT)
					{
						currentDirection = Directions.NONE;
					}
					break;
				case Keyboard.LEFT :
					if (currentDirection == Directions.LEFT)
					{
						currentDirection = Directions.NONE;
					}
					break;
			}
		}

	}

}